﻿/*
 * XBMC Connect - A .Net library to remotely connect to XMBC
 * Copyright (C) 2010 - 2011 Pieter De Rycke
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JsonRpc.Generator.Tests
{
    class TestData
    {
        [CommandLineArgument("output")]
        public string Output { get; set; }

        [CommandLineArgument("merge")]
        public bool MergeConfig { get; set; }

        [CommandLineArgument("count")]
        public int Count { get; set; }

        [CommandLineArgument(IsDefaultArgument = true)]
        public string FileName { get; set; }
    }

    class InvalidTestData
    {
        [CommandLineArgument("invalid")]
        public Guid Invalid { get; set; }
    }

    [TestClass]
    public class CommandLineParserTests
    {
        [TestMethod]
        public void NewCommandLineParser_WithEmptyInput()
        {
            CommandLineParser parser = new CommandLineParser(new string[0]);
        }

        [TestMethod]
        public void Fill_OneNamedArgumentWithValue()
        {
            CommandLineParser parser = new CommandLineParser(new string[] { "/output:test.bla", "test.config" });

            TestData o = new TestData();
            parser.Fill(o);

            Assert.AreEqual("test.bla", o.Output);
        }

        [TestMethod]
        public void Fill_OneNamedArgumentWithoutValue()
        {
            CommandLineParser parser = new CommandLineParser(new string[] { "/merge", "test.config" });

            TestData o = new TestData();
            parser.Fill(o);

            Assert.IsTrue(o.MergeConfig);
        }

        [TestMethod]
        public void Fill_OneNamedArgumentWithIntegerValue()
        {
            CommandLineParser parser = new CommandLineParser(new string[] { "/count:5", "test.config" });

            TestData o = new TestData();
            parser.Fill(o);

            Assert.AreEqual(5, o.Count);
        }

        [TestMethod]
        public void Fill_DefaultArgument()
        {
            CommandLineParser parser = new CommandLineParser(new string[] { "/count:5", "test.config" });

            TestData o = new TestData();
            parser.Fill(o);

            Assert.AreEqual("test.config", o.FileName);
        }

        [TestMethod]
        [ExpectedException(typeof(CommandLineArgumentException))]
        public void Fill_InvalidProperty()
        {
            CommandLineParser parser = new CommandLineParser(new string[] { "/count:5", "test.config" });

            InvalidTestData o = new InvalidTestData();
            parser.Fill(o);
        }

        [TestMethod]
        public void GetArgument()
        {
            CommandLineParser parser = new CommandLineParser(new string[] { "/output:test.bla", "test.config" });

            Assert.AreEqual("test.bla", parser["output"]);
        }
    }
}
